<?
function updateToken( & $config )
{
        require_once 'SDK/OpenTokSDK.php';

        $partnerId = API_Config::API_KEY;
        $partnerSecret = API_Config::API_SECRET;
        $apiurl= API_Config::API_SERVER;

        //echo "partnerId|$partnerId|<br>";
        //echo "partnerId|$partnerSecret|<br>";
        //echo "url|$apiurl|<br>";
        $apiObj = new OpenTokSDK($partnerId, $partnerSecret);

        $createSessionString = $apiObj->create_session('208.117.3.16');
        $config["SESSIONID"] = $createSessionString->getSessionId();

        $config["TOKEN"] = $apiObj->generate_token();
        //print "sessionid: $sessionId<br>token: $token";
}

   function SaveConfig($configName,$config )
   {
      global $tb_basepath;
      $file = "$tb_basepath/config/$configName.ini";
      $fd = fopen ($file , "w");
  	if ( ! $fd )
  	{
        return null;
 	 }

      foreach ($config as $key => $value) {
	fputs( $fd, "$key=$value\n");
      }
      fclose ($fd);
   }


   function UploadFile (  & $config )
   {
        global $tb_basepath;
	if ( $_FILES['uploadbanner']['name'] != null && $_FILE['uploadbanner']['error']  == UPLOAD_ERR_OK ) 
	{
		$basename= basename ( $_FILES['uploadbanner']['name'] );
		$ext = strtolower( pathinfo($basename, PATHINFO_EXTENSION) );
		$validext=false;
		if ( $ext == "jpg" || ext == "jpeg" )
		{
			$ext="jpg";
			$validext=true;
		}
		if ( $validext == true )
		{
			$unixtime = time();
			$bannername="$tb_basepath/config/banner_" . $unixtime . ".$ext"; 
			if(move_uploaded_file($_FILES['uploadbanner']['tmp_name'], $bannername)) {
				$config["UPLOADBANNER"]=basename ( $bannername);
			}
		}
	}
   }
   function UpdatePost2Config( & $config, $str )
   {

      UploadFile ( $config);

      foreach ( $_POST as $key=>$value)
      {
         if ( strstr($key,$str)  )
         {
		$newkey=str_replace ( $str,"",$key);
		$config["$newkey"]="$value";
         }
      }
   } 
   function PrintConfig($config)
   {
	print "post:<b>";
      foreach ( $_POST as $key=>$value){
         print "key:$key val:$value<br>";
      }
	print "config:<b>";

      foreach ($config as $key => $value) {
         print "key:$key val:$value<br>";
      }
   }


function ReadConfig($configName)
{
  global $tb_basepath;
  $file = "$tb_basepath/config/$configName.ini";
  if  (!file_exists($file)) {
  	$file = "$tb_basepath/config/tbconfig.ini";
  }
  if  (!file_exists($file)) {
      return null;
  }

  $fd = fopen ($file , "r");

  if ( ! $fd )
  {
        return null;
  }

  $config = array ();
  $count=0;
  while (!feof ($fd))
  {
      $data= fgets($fd, 4096);
      $data = rtrim($data, "\r\n");
      $data = rtrim($data, "\n");
      if ( $data != "" )
      {
         list($key, $value) = explode("=", $data,2);
         $config["$key" ] = $value;
      }
      $count++;
      if ( $count>30)
	break;
  }
  fclose ($fd);
  return $config;
}

?>
